package com.lzx.user.controller;

import com.lzx.common.utils.R;
import com.lzx.user.entity.SysUser;
import com.lzx.user.entity.vo.SearchParams;
import com.lzx.user.service.SysUserService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzx
 * @since 2022-11-22
 */
@RestController
@RequestMapping("/user/sysUser")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    //后台登录
    @PostMapping("/login")
    public R login(@RequestBody SysUser sysUser, HttpServletRequest request) {
        return sysUserService.login(sysUser, request);
    }

    //获取所有用户
    @GetMapping("/getSysUserList/{current}/{size}")
    public R getSysUserList(@PathVariable int current, @PathVariable int size) {
        return sysUserService.getSysUserList(current, size);
    }

    //新增用户
    @PostMapping("/addSysUser")
    public R addSysUser(@RequestBody @Valid SysUser sysUser, BindingResult result) {
        if (result.hasErrors()) {
            return R.error().message(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return sysUserService.addSysUser(sysUser);
    }

    //修改用户信息
    @PutMapping("/updateSysUser")
    public R updateSysUser(@RequestBody @Valid SysUser sysUser, BindingResult result) {
        if (result.hasErrors()) {
            return R.error().message(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return sysUserService.updateSysUser(sysUser);
    }

    //禁用用户
    @PutMapping("/deleteSysUser/{ids}")
    public R deleteSysUser(@PathVariable List<Long> ids) {
        return sysUserService.deleteSysUser(ids);
    }

    //解禁用户
    @PutMapping("/relieveSysUser/{id}")
    public R relieveSysUser(@PathVariable Long id) {
        return sysUserService.relieveSysUser(id);
    }

    //根据id获取用户信息
    @GetMapping("/{id}")
    public R getSysUser(@PathVariable Long id) {
        return sysUserService.getSysUser(id);
    }

    //搜索
    @PostMapping("/searchSysUser")
    public R searchSysUser(@RequestBody SearchParams searchParams) {
        return sysUserService.searchSysUser(searchParams);
    }

    //根据token获取个人信息
    @GetMapping("/getSysUserInfo")
    public R getSysUserInfo(HttpServletRequest request) {
        return sysUserService.getSysUserInfo(request);
    }

    //导出
    @GetMapping("/exportSysUser")
    public void exportSysUser(HttpServletResponse response) {
        sysUserService.exportSysUser(response);
    }
}
